#include "loginform.h"
#include "ui_loginform.h"
#include "mydatebase.h"
#include "formsrv.h"
#include <QDateTime>
#include "logmsg.h"
LoginForm::LoginForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginForm)
{
    ui->setupUi(this);
    this->setWindowFlags(this->windowFlags() &~(Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint));
}

LoginForm::~LoginForm()
{
    delete ui;
}

void LoginForm::setGBoxTitle(QString title)
{
    ui->groupBox_LogIn->setTitle(title);
    if(title == "用户登录")
    {
        ui->label_LogIn_3->setVisible(false);
        ui->lineEdit_ConfirmKey->setVisible(false);
    }else
    {
        ui->pushButton_OK->setText("确认添加");
    }
}

void LoginForm::StartLogin()
{
    if(getDB()->GetUserCount() <= 0)
    {
        //qDebug()<<"cout:"<<getDB()->GetUserCount();
        ui->stackedWidget->setCurrentIndex(1);
        ui->groupBox_LogIn_2->setTitle("添加账户");
        ui->label_LogIn_4->setText("管理员账户：");
        ui->label_LogIn_5->setText("管理员密码：");
        ui->pushButton_Admin->setText("确定");
    }
    else
    {
        ui->stackedWidget->setCurrentIndex(1);
        ui->groupBox_LogIn_2->setTitle("输入账号及密码登录");
        ui->label_LogIn_4->setText("用户名：");
        ui->label_LogIn_5->setText("密码：");
        ui->pushButton_Admin->setText("登录");
    }
}
void LoginForm::on_pushButton_Admin_clicked()
{
    if(ui->lineEdit_UserName_2->text().length() == 0 || ui->lineEdit_UserKey_2->text().length() == 0)
    {
        return;
    }
    if(ui->pushButton_Admin->text() == "确定")
    {
        if(ui->lineEdit_UserName_2->text() != "admin" || ui->lineEdit_UserKey_2->text() != "admin"+QDateTime::currentDateTime().toString("hhmm"))
        {
            getLogMsg()->iLogMsg(tr("管理员登录失败,用户名：%1,,密码：%2").arg(ui->lineEdit_UserName_2->text()).arg(ui->lineEdit_UserKey_2->text()));
            return;
        }
        ui->stackedWidget->setCurrentIndex(0);
    }
    else
    {
        QString UserName = ui->lineEdit_UserName_2->text();
        if(getDB()->CheckUser(UserName,ui->lineEdit_UserKey_2->text()))
        {
            this->close();
            //getForm()->show();
            getForm()->slot_LoadRoadInfo();
            getForm()->showMaximized();
            getForm()->SetLogInfo(UserName,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
            getLogMsg()->iLogMsg(tr("用户：%1登录监控系统").arg(UserName));
        }else
        {
            getLogMsg()->iLogMsg(tr("登录监控系统失败,用户名：%1,密码：%2").arg(UserName).arg(ui->lineEdit_UserKey_2->text()));
        }
    }
}
void LoginForm::on_pushButton_OK_clicked()
{
    QString UserName = "";
    if(ui->lineEdit_UserName->text().length() == 0 || ui->lineEdit_UserKey->text().length() == 0 || ui->lineEdit_ConfirmKey->text().length() == 0)
    {
        return;
    }
    if(ui->lineEdit_ConfirmKey->text() != ui->lineEdit_UserKey->text())
    {
        return;
    }
    if(!getDB()->CreateUser(ui->lineEdit_UserName->text(),ui->lineEdit_UserKey->text()))
    {
        return;
    }
    UserName = ui->lineEdit_UserName->text();
    this->close();
    //getForm()->show();
    getForm()->slot_LoadRoadInfo();
    getForm()->showMaximized();
    getForm()->SetLogInfo(UserName,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    getLogMsg()->iLogMsg(tr("创建用户：%1并登录监控系统").arg(UserName));
}

void LoginForm::on_pushButton_Cancle_clicked()
{
    getDB()->CloseDB();
    this->close();
}
void LoginForm::on_pushButton_Cancle_2_clicked()
{
    getDB()->CloseDB();
    this->close();
}
